package com.cmas.platform.controller;

import java.util.Arrays;
import java.util.List;
import java.util.HashMap;
import java.util.Map;
import com.aidex.common.annotation.DataSource;
import com.aidex.common.annotation.Log;
import com.aidex.common.core.domain.R;
import com.aidex.common.core.page.TableDataInfo;
import com.aidex.common.core.page.PageDomain;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.aidex.common.enums.BusinessType;
import com.aidex.common.enums.DataSourceType;
import com.aidex.common.utils.SecurityUtils;
import com.aidex.common.utils.poi.ExcelUtil;
import com.aidex.common.core.controller.BaseController;
import com.aidex.framework.cache.DictUtils;
import javax.validation.constraints.*;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cmas.platform.vo.UserRecordVo;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import com.cmas.platform.domain.UserRecord;
import com.cmas.platform.service.UserRecordService;


/**
 * 用户记录表Controller
 * @author su
 * @email 1218819874@qq.com
 * @date 2023-04-28
 */
@DataSource(value = DataSourceType.SLAVE)
@RestController
@RequestMapping("/platform/userRecord")
public class UserRecordController extends BaseController  {

    @Autowired
    private UserRecordService userRecordService;

    /**
     * 查询用户记录表列表
     */
    @PreAuthorize("@ss.hasPermi('platform:userRecord:list')")
    @GetMapping("/list")
    public R<TableDataInfo> list(UserRecord userRecord) {
        startPage();
        List<UserRecordVo> list = userRecordService.selectUserRecordVoList(userRecord);
        return R.data(getDataTable(list));
    }

    /**
     * 获取用户记录表详细信息
     */
    @PreAuthorize("@ss.hasPermi('platform:userRecord:query')")
    @GetMapping(value = "/{id}")
    public R<UserRecord> detail(@PathVariable("id") String id) {
        return R.data(userRecordService.getById(id));
    }

    /**
     * 新增用户记录表
     */
    @PreAuthorize("@ss.hasPermi('platform:userRecord:add')")
    @Log(title = "用户记录表", businessType = BusinessType.INSERT)
    @PostMapping
    public R add(@RequestBody @Validated  UserRecord userRecord) {
        return R.data(userRecordService.save(userRecord));
    }

    /**
     * 修改用户记录表
     */
    @PreAuthorize("@ss.hasPermi('platform:userRecord:edit')")
    @Log(title = "用户记录表", businessType = BusinessType.UPDATE)
    @PutMapping
    public R edit(@RequestBody @Validated UserRecord userRecord) {
        return R.data(userRecordService.updateById(userRecord));
    }

    /**
     * 删除用户记录表
     */
    @PreAuthorize("@ss.hasPermi('platform:userRecord:remove')")
    @Log(title = "用户记录表", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R remove(@PathVariable String[] ids) {
        return R.data(userRecordService.removeByIds(Arrays.asList(ids)));
    }


    /**
     * 导出用户记录表列表
     */
    @PreAuthorize("@ss.hasPermi('platform:userRecord:export')")
    @Log(title = "用户记录表", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public R export(UserRecord userRecord) {
        List<UserRecord> list = userRecordService.selectUserRecordList(userRecord);
        ExcelUtil<UserRecord> util = new ExcelUtil<UserRecord>(UserRecord.class);
        return util.exportExcel(list, "用户记录表数据");
    }
	
    /**
     * 根据字典类型查询字典数据信息等其他自定义信息
     */
    @GetMapping(value = "/getInitData/{dictTypes}")
    public R getInitData(@PathVariable String dictTypes) {
        Map<String, Object> dataMap = new HashMap<String, Object>();
        dataMap.putAll(DictUtils.getMultiDictList(dictTypes));
        return R.data(dataMap);
    }

    // ***用户系统位置戳****

    @GetMapping("/userRecordByOnlineUser")
    public R<?> userRecordByOnlineUser() {
        return R.data(userRecordService.getOne(new QueryWrapper<UserRecord>().lambda()
                .eq(UserRecord::getUserId, SecurityUtils.getUserId())));
    }


    @GetMapping("/userInfoRecord")
    public R<?>  userInfoRecord(@RequestParam(name = "userId") String userId) {
        return R.data(userRecordService.getUserInfoRecord(userId));
    }

}
